//http://192.168.0.159:8080/Telemetering/telemetering/one.jsp?&cardid=130434198901010451
var oldPoint = -1;
  if ("WebSocket" in window)
  {
     // Let us open a web socket
     var ws = new WebSocket("ws://192.168.0.159:8989");
     ws.onopen = function()
     {
        // Web Socket is connected, send data using send()
    	var cardid = document.getElementById('jsp_js_id').value;
    	var jsonStr = getJson("first",cardid);
        ws.send(jsonStr);
        console.log("cardid="+cardid +" send end!");
     };
     ws.onmessage = function (evt)
     {
//    	 console.log("接收->:" + evt.data);
        var received_msg = eval('('+evt.data+')');
//        console.log("type=" + received_msg["type"]);
        
        var type = received_msg["type"];
        if(type == "first"){
        	var name = received_msg["name"];
            var bednum = received_msg["bednum"];
            var channel = received_msg["channel"];
            
            if(getByClass("username")[0].innerHTML != "姓名:"+name){
            	getByClass("username")[0].innerHTML = "姓名:"+name;
            }
            
            if(getByClass("userbednumber")[0].innerHTML != "床号:"+bednum){
            	getByClass("userbednumber")[0].innerHTML = "床号:"+bednum;
            }
            
        }
        else if(type == "data"){
//        	var hr = received_msg["hr"];
        	if(document.getElementById("hrid").innerHTML != received_msg["hr"] && received_msg["hr"] != 0){
        		document.getElementById("hrid").innerHTML = received_msg["hr"];
        	}
        	if(document.getElementById("stid").innerHTML != received_msg["st"] && received_msg["st"] != 0){
        		document.getElementById("stid").innerHTML = received_msg["st"];
        	}
        	if(document.getElementById("spo2id").innerHTML != received_msg["spo2"] && received_msg["spo2"] != 0){
        		document.getElementById("spo2id").innerHTML = received_msg["spo2"];
        	}
        	if(document.getElementById("rrid").innerHTML != received_msg["rr"] && received_msg["rr"] != 0){
        		document.getElementById("rrid").innerHTML = received_msg["rr"];
        	}
        	if(document.getElementById("prid").innerHTML != received_msg["pr"] && received_msg["pr"] != 0){
        		document.getElementById("prid").innerHTML = received_msg["pr"];
        	}
        	if(document.getElementById("nibpid").innerHTML != received_msg["nibp1"] +"/" + received_msg["nibp2"] && received_msg["nibp1"] != 0 && received_msg["nibp2"] != 0){
        		document.getElementById("nibpid").innerHTML = received_msg["nibp1"] +"/" + received_msg["nibp2"];
        	}
        	if(document.getElementById("centerid").innerHTML != received_msg["nibp3"] && received_msg["nibp3"] != 0){
        		document.getElementById("centerid").innerHTML = received_msg["nibp3"];
        	}
        	if(document.getElementById("t1id").innerHTML != received_msg["t1"] && received_msg["t1"] != 28){
        		var t1Num = received_msg["t1"]+"";
        		if(t1Num.length > 4){
        				document.getElementById("t1id").innerHTML = t1Num.substring(0,4);
        		}else{
        			document.getElementById("t1id").innerHTML = t1Num;
        		}
        	}
        	if(document.getElementById("t2id").innerHTML != received_msg["t2"] && received_msg["t2"] != 28){
        		var t2Num = received_msg["t2"]+"";
        		if(t2Num.length > 4){
        			document.getElementById("t2id").innerHTML = t2Num.substring(0,4);
        		}else{
        			document.getElementById("t2id").innerHTML = t2Num;
        		}
        	}
        }
        else if(type == "wave"){
//        	console.log("re  message! ->"+received_msg);
        	/*
        	 * 更改下拉菜单
        	var allChannel = received_msg["allChannel"].split(",");//接收到的内容
        	//下拉菜单
        	document.getElementById("select").options.length=0;//删除所有
        	
//        	console.log("-->"+allChannel+"   length"+allChannel.length);
        	for(var i=0; i<allChannel.length; i++){
        		if(allChannel[i] != "" && allChannel[i] != null && allChannel[i] != undefined){
//        			console.log("遥测"+allChannel[i]+"end");
        			document.getElementById("select").options.add(new Option("遥测"+allChannel[i],allChannel[i]+""));//添加一个
//        			document.getElementById("select").options[document.getElementById("select").selectedIndex].value;
        		}
        	}
        	*/
        	//绘制
        	var wave2 = received_msg["wave2_"+selectNum];
        	if(wave2 != null && wave2 != undefined){
//        			console.log("--->"+wave2);
//        			if(wave2 <= 150){
        				oldPoint = wave2;
//        			}
        			show(oldPoint, 1);
        	}
        	var wave5 = received_msg["wave5_"+selectNum];
        	if(wave5 != null && wave5 != undefined){
        			show(wave5, 2);
        	}
        }
        
     };
     ws.onclose = function()
     {
        alert("Connection is closed...");
     };
  }
  else
  {
     alert("WebSocket NOT supported by your Browser!");
  }

var selectNum = 1;
function selectChange(){
	var text = document.getElementById("select").options[document.getElementById("select").selectedIndex].text;
//	alert(text);
//	var value = document.getElementById("select").options[document.getElementById("select").selectedIndex].value;
//	alert(value);
//	ws.send(getJsonSelect(text));
	selectNum = parseInt(text.substr(2,2));
	
//	document.getElementById("select").options.length=0;//删除所有
//	document.getElementById("select").options.remove(1);//删除一个
//	document.getElementById("select").options.add(new Option("111","yiyiyi"));//添加111
//	document.getElementById("select").options[document.getElementById("select").selectedIndex].text;//获取文本
}

/**
 * json
 * {"type":"first|","cardid":"10100...21"}
 */
function getJson(type,cardid){
	return "{\"type\":\""+type+"\",\"cardid\":\""+cardid+"\"}";
}
/**
 * json
 */
function getJsonSelect(text){
	return "{\"type\":\"select\",\"select\":\""+text+"\"}";
}

function getByClass(sClass){
    var aResult=[];
    var aEle=document.getElementsByTagName('*');
    for(var i=0;i<aEle.length;i++){
       /*当className相等时添加到数组中*/
       if(aEle[i].className==sClass){
            aResult.push(aEle[i]);
        }
    }
    return aResult;
};
